package com.shujia.wyh.controller;

import com.shujia.wyh.entity.Emp;
import com.shujia.wyh.service.EmpService;
import com.shujia.wyh.service.impl.EmpServiceImpl;
import com.shujia.wyh.utils.MySqlTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;
import java.util.List;

@Controller
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpServiceImpl empService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @RequestMapping("add")
    public String add() {
        return "add";
    }

    @PostMapping("add2")
    public String add2(Emp emp, Model model) {
        int add = empService.add(emp);
        if (add > 0) {
            System.out.println("数据插入成功!!");
            List<Emp> emps = empService.findAll();
            model.addAttribute("emps", emps);
            return "shouye";
        } else {
            return "add";
        }
    }

    @RequestMapping("delete/{empno}")
    public String delete(@PathVariable("empno") String empno, Model model) {
        empService.delete(empno);
        List<Emp> emps = empService.findAll();
        model.addAttribute("emps", emps);
        return "shouye";
    }

    @RequestMapping("updateEmp/{empno}")
    public String updateEmp(@PathVariable("empno") String empno, Model model) {
        //根据员工编号查该员工的信息
        Emp emp = empService.findByEmpno(empno);
        model.addAttribute("emp", emp);
        return "update";
    }

    @PostMapping("update")
    public String updateEmp2(Emp emp, Model model) {
        empService.updateEmp(emp);
        List<Emp> emps = empService.findAll();
        model.addAttribute("emps", emps);
        return "shouye";
    }

    @PostMapping("selectOne")
    public String selectOne(Emp emp, Model model) {
        //先去redis中查看数据
        Object o = redisTemplate.opsForValue().get(emp.getEmpno());
        if (o == null) {
            System.out.println("====================走mysql的============================");
            //如果redis中没有数据,直接查mysql数据库
            Emp emp2 = empService.findByEmpno(emp.getEmpno());
            //查到之后,往redis中插入该数据
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.opsForValue().set(emp2.getEmpno(),emp2);
            //设置过期时间
            redisTemplate.expire(emp2.getEmpno(), Duration.ofSeconds(60L));
            model.addAttribute("emp",emp2);
            return "selectone";
        }else {
            System.out.println("====================走redis的============================");
            Object o1 = redisTemplate.opsForValue().get(emp.getEmpno());
            //向下转型
            Emp emp2 = (Emp) o1;
            model.addAttribute("emp",emp2);
            return "selectone";
        }


//        Emp emp2 = empService.findByEmpno(emp.getEmpno());
//        model.addAttribute("emp",emp2);
    }
}
